/**
 * 附件
 */
function AttachmentModule(ui, attachmentCtl, engine) {
	this.base = Module;
	this.base(ui, engine);

	this.stateDesc = "";

	this.attachmentCtl = (typeof (attachmentCtl) == "undefined" || attachmentCtl == null) ? false
			: attachmentCtl;

	if(this.attachmentCtl)
		this.state = false;
	else//若不需要上传附件，则状态ok
		this.state=true;
}
AttachmentModule.prototype = new Module();

AttachmentModule.prototype.isOk = function() {
	return true;//附件模块不需要判断状态
	//return (this.state == null || typeof (this.state) == 'undefined') ? false:this.state;
};

AttachmentModule.prototype.alert = function() {
	dialog.alert("请上传附件后提交！");
};

AttachmentModule.prototype.init = function() {
	var attachmentModule = this;
	this.getFileList();
	// 上传组件初始化
	if (this.attachmentCtl) {
		var uploadInputElement = $(".stream_upload");
		if (uploadInputElement.length > 0) {
			initStreamConfig({
				basePath : ctxPath,
				browseFileBtn : '<a title="添加附件" class="a-btn"> <span class="fa fa-plus"></span><span class="text">添加附件</span></a>',
				afterUpload : function(fileId) {// 上传完图片的回调函数
					attachmentModule.upload(fileId);
					$("#i_stream_message_container0").remove();
				}
			});
		}
	}
};
/**
 * 附件上传
 */
AttachmentModule.prototype.upload = function(fileId) {
	var action = ctxPath + "/jbpmService/attachment/saveUploadFile";
	var engine = this.engine;
	var attachmentModule = this;
	var jsonData = "workId=" + engine.workId + "&todoId=" + engine.todoId
			+ "&fileId=" + fileId;
	console.log("开始上传 jsonData:" + jsonData);
	$.ajax({
		type : "POST",
		url : action,
		data : jsonData,
		success : function(data) {
			this.state = true;
			attachmentModule.getFileList();
		},
		error : function(data) {
			dialog.alert(data);
		}
	});
};
/**
 * 附件下载 暂未试用
 */
AttachmentModule.prototype.download = function(fileId) {
	var action = ctxPath + "/sys/file!download.action?fileId=" + fileId;
	var engine = this.engine;
};
/**
 * 附件删除
 */
AttachmentModule.prototype.delFile = function(attachId) {
	var action = ctxPath + "/jbpmService/attachment/deleteAttach";
	var engine = this.engine;
	var jsonData = "workId=" + engine.workId + "&attachId=" + attachId;
	console.log("开始附件删除 jsonData:" + jsonData);
	$.post(action, jsonData, function(data) {
		console.log(data);
		engine.attachmentModule.getFileList();
	});
};
/**
 * 附件预览
 */
AttachmentModule.prototype.preView = function() {
	var action = ctxPath + "/jbpmService/attachment";
	var engine = this.engine;

};
/**
 * 获取当前工单附件表
 */
AttachmentModule.prototype.getFileList = function() {
	console.log("开始加载附件列表");
	var action = ctxPath + "/jbpmService/attachment/show";
	var engine = this.engine;
	var todoId=engine.todoId;
	var uiObj = this.getUI();
	var attachmentModule=this;
	uiObj.find("tbody").empty();
	$ .post(
					action,
					"workId=" + engine.workId,
					function(data) {
						var dataObj = Util.strToObj(data);
						if (dataObj) {
							for (var i = 0; i < dataObj.length; i++) {

								if(dataObj[i].todoId==todoId)//说明已经上传过附件
									attachmentModule.state=true;

								var tr = '<tr>';
								tr += '<td>' + dataObj[i].sysFile.oldName
										+ '</td>';
								tr += '<td>' + dataObj[i].handlerName + '</td>';
								tr += '<td>' + dataObj[i].activityName
										+ '</td>';
								tr += '<td>' + dataObj[i].uploadTime + '</td>';
								tr += '<td><a href="'
										+ ctxPath
										+ '/sys/file!download.action?fileId='
										+ dataObj[i].fileId
										+ '" title="附件下载"><span class="fa fa-download"></span></a>&nbsp;&nbsp;';
								if(attachmentModule.attachmentCtl){//有操作权限
									tr += '<a href="javascript:void(0)" onclick="engine.attachmentModule.delFile(\''
											+ dataObj[i].attachId
											+ '\')" title="附件删除"><span class="fa fa-remove"></span></a>&nbsp;&nbsp;';
								}
								tr += '<a href="javascript:void(0)" title="附件预览"><span class="fa fa-television"></span></a></td>';
								tr += '</tr>';
								uiObj.find("tbody").append(tr);
							}
						}
					});
};
